package com.kehutong.house.app;

import com.kehutong.house.entity.Community;
import com.kehutong.house.entity.Shop;
import com.kehutong.house.entity.ShopClassify;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.util.Objects;

import java.util.List;

/**
 * 商铺信息
 */
@MvcAction
@ReqMapping("/kht-bin/basic/shop")
public class ShopAction {

    @Inject
    private JdbcSession jdbcSession;


    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
        List<Shop> shops = jdbcSession.findArray(Shop.class)
                .eq(jsonObject,"id","communityNo", "shopClassifyNo")
                .eq("deleted", false)
                .order("shopClassifyName","ASC")
//                .order("floorName","ASC")
                .order("name","ASC")
                .exe();

        shops.forEach(item->{
            String name = item.getOwnerName();
            if(Objects.nonEmpty(name)){
                if (name.length() > 2) {
                    item.setOwnerName(name.substring(0, 1) + "*" + name.substring(2));
                }else {
                    item.setOwnerName(name.substring(0, 1) + "*");
                }
            }else{
                item.setOwnerName("物业公司");
            }
        });

        return Result.success(shops);
    }

    @ReqMapping("/get")
    public Object get(JSONObject jsonObject) {
        Shop shop = Pool.get(Shop.class,  jsonObject.getString("id"));

        String name = shop.getOwnerName();
        if(Objects.nonEmpty(name)){
            if (name.length() > 2) {
                shop.setOwnerName(name.substring(0, 1) + "*" + name.substring(2));
            }else {
                shop.setOwnerName(name.substring(0, 1) + "*");
            }
        }else{
            shop.setOwnerName("物业公司");
        }
        Community community = Pool.get(Community.class, shop.getCommunityNo());

        ShopClassify shopClassify = Pool.get(ShopClassify.class, shop.getShopClassifyNo());
        JSONObject result = new JSONObject();
        result.put("shop", shop);
        result.put("community", community);
        result.put("shopClassify", shopClassify);

        return Result.success(result);
    }
}
